package rn.automacao.persistencia;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import rn.automacao.banco.Conexao;
import rn.automacao.dominio.TipoVO;

public class TipoDAO {

	PreparedStatement ps = null;

	Connection conn = null;

	ResultSet rs = null;

	public Object pesquisarTipoFuncionario(int param, String filtro) {

		conn = Conexao.getInstance().abreConexao();
		try {
			ps = conn.prepareStatement("SELECT * FROM TIPOFUNCIONARIO WHERE "+filtro+" = "+param);
			rs = ps.executeQuery();

			rs.next();
			return new TipoVO(rs.getInt(1), rs.getString(2));

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				ps.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public Object pesquisarTipoOS(int param, String filtro) {

		conn = Conexao.getInstance().abreConexao();
		try {
			ps = conn.prepareStatement("SELECT * FROM TIPOOS WHERE "+filtro+" = "+param);
			rs = ps.executeQuery();

			rs.next();
			return new TipoVO(rs.getInt(1), rs.getString(2));

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				ps.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return null;
	}


	public Collection<Object> pesquisarTipoOS(String param, String filtro) {
		Collection<Object> tiposOS = new ArrayList<Object>();
		conn = Conexao.getInstance().abreConexao();

		try {
			ps = conn.prepareStatement("SELECT * FROM TIPOOS WHERE "+filtro+" LIKE '%"+param+"%'");
			rs = ps.executeQuery();

			while (rs.next()){
				tiposOS.add(new TipoVO(rs.getInt(1), rs.getString(2)));
			}

			return tiposOS;

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				ps.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

}
